package ru.susu.algebra.chartable.constructor;

import java.math.BigInteger;
import java.util.List;

import ru.susu.algebra.chartable.degree.HooksDegreeCalculator;
import ru.susu.algebra.pair.Pair;
import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.partition.PartitionsFacade;

/**
 * @author akargapolov
 * @since: 03.03.2009
 */
public abstract class SymmetricGroupTableConstructorBase extends PermGroupTableConstructorBase<BigInteger>
{

	@Override
	public BigInteger calcValue(Partition p1, Partition p2) throws Exception
	{
		BigInteger res = BigInteger.ZERO;

		if (p2.getPresentedNumber().equals(p2.getArrayRepresentation().length))
		{
			return new HooksDegreeCalculator().calculateDegree(p1);
		}

		Partition p22 = PartitionsFacade.getWithoutLastWithCache(p2);

		List<Pair<Partition, Integer>> pairs = PartitionsFacade.getPartitionsWithCache(p1, p2);

		for (Pair<Partition, Integer> pair : pairs)
		{
			Partition p = pair.getKey();
			Integer st = pair.getValue();
			BigInteger tRes = BigInteger.valueOf((int) Math.pow(-1.0, st));
			if (p.getLength() > 0)
			{
				tRes = tRes.multiply(this.getValue(p, p22));
			}
			res = res.add(tRes);
		}
		return res;
	}
}
